home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE12 / CLINIC / RAWDATA.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1996-05-13  |  2.0 KB  |  84 lines

  1. unit RawData;
  2.  
  3. {$ifndef Win32}
  4.   'This is for Delphi 2/Win32 only'
  5. {$endif}
  6.  
  7. interface
  8.  
  9. // RawDataToPrinter - sends binary data directly to a printer
  10. //
  11. // Params:
  12. //   PrinterName - printer name
  13. //   Data        - raw data bytes - any variable will do
  14. //   Count       - length of Data in bytes
  15. //
  16. // Gives an exception upon failure
  17. // (with Break on Exception on it may give several)
  18. procedure RawDataToPrinter(PrinterName: String; const Data; Count: Integer);
  19.  
  20. implementation
  21.  
  22. uses
  23.   WinSpool, Windows, Consts, SysUtils;
  24.  
  25. type
  26.   EPrinterError = class(Exception);
  27.  
  28. procedure Error;
  29. begin
  30.   raise EPrinterError.CreateRes(SInvalidPrinterOp);
  31. end;
  32.  
  33. procedure RawDataToPrinter(PrinterName: String; const Data; Count: Integer);
  34. type
  35.   TDoc_Info_1 = record
  36.     DocName,
  37.     OutputFile,
  38.     Datatype: PChar;
  39.   end;
  40. var
  41.   hPrinter: THandle;
  42.   DocInfo: TDoc_Info_1;
  43.   BytesWritten: Integer;
  44. begin
  45.   // Need a handle to the printer
  46.   if not OpenPrinter(PChar(PrinterName), hPrinter, nil) then
  47.     Error;
  48.   // Fill in the structure with info about this "document"
  49.   DocInfo.DocName := 'My Document';
  50.   DocInfo.OutputFile := nil;
  51.   DocInfo.Datatype := 'RAW';
  52.   try
  53.     // Inform the spooler the document is beginning
  54.     if StartDocPrinter(hPrinter, 1, @DocInfo) = 0 then
  55.       Error;
  56.     try
  57.       // Start a page
  58.       if not StartPagePrinter(hPrinter) then
  59.         Error;
  60.       try
  61.         // Send the data to the printer
  62.         if not WritePrinter(hPrinter, @Data, Count, BytesWritten) then
  63.           Error;
  64.       finally
  65.         // End the page
  66.         if not EndPagePrinter(hPrinter) then
  67.           Error;
  68.       end;
  69.     finally
  70.       // Inform the spooler that the document is ending
  71.       if not EndDocPrinter(hPrinter) then
  72.         Error;
  73.     end;
  74.   finally
  75.     // Tidy up the printer handle
  76.     ClosePrinter(hPrinter);
  77.   end;
  78.   // Check to see if correct number of bytes written
  79.   if BytesWritten <> Count then
  80.     Error;
  81. end;
  82.  
  83. end.
  84.